use semx_bitops::{
    bit, bit_ull, bitmap::BitMap, bits_to_longs, clear_bit, extract32, find_last_bit,
    make_64bit_mask, set_bit, BITS_PER_LONG,
};

#[test]
fn bitops_test() {
    #[cfg(target_pointer_width = "64")]
    assert_eq!(BITS_PER_LONG, 64);

    #[cfg(target_pointer_width = "32")]
    assert_eq!(BITS_PER_LONG, 32);

    assert_eq!(1, bit(0));
    assert_eq!(2, bit(1));
    assert_eq!(4, bit(2));

    let bits = bit(0);
    assert_eq!(bits, 1);
    let bits = bit(31);
    assert_eq!(bits, 0x80000000);

    assert_eq!(1 << 63, bit_ull(63));

    assert_eq!(1, semx_bitops::bit_mask(0));
    assert_eq!(8, semx_bitops::bit_mask(3));

    let bit_mask = semx_bitops::bit_mask(64);
    assert_eq!(bit_mask, 1);

    let bit_word = semx_bitops::bit_word(2);
    assert_eq!(bit_word, 0);

    let bit_word = semx_bitops::bit_word(65);
    assert!(bit_word == 1 || bit_word == 2);

    assert_eq!(bits_to_longs(1), 1);
    assert_eq!(bits_to_longs(31), 1);

    assert_eq!(make_64bit_mask(0, 1), 1);
    assert_eq!(make_64bit_mask(0, 2), 0b11);
    assert_eq!(make_64bit_mask(1, 3), 0b1110);

    let mut bit = [0b101010111101; 1];
    set_bit(1, &mut bit);
    assert_eq!(bit[0], 0b101010111111);
    clear_bit(5, &mut bit);
    assert_eq!(bit[0], 0b101010011111);

    let mut bit = [0b101010111101, 0b0000];
    set_bit(64, &mut bit);
    assert_eq!(bit[1], 1);
    let index = find_last_bit(&bit, 128);
    assert_eq!(index, 64);

    let value = 0x8000008Eu32;
    let ex = extract32(value, 1, 3);
    assert_eq!(ex, 7);

    let mut bitmap = BitMap::create(4);
    bitmap.set_fill();
    assert_eq!(bitmap.count_one(), 4);
    bitmap.set_zero();
    assert_eq!(bitmap.count_one(), 0);

    bitmap.bitmap_set(0, 2);
    assert_eq!(bitmap.count_one(), 2);
}
